import pygame
import time
from .. import tools,setup
from .. import constants as C

class idleCoin(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.frames=[]
        self.frameIndex=0
        frameRects=[(0,0,16,16),(16,0,16,16),(32,0,16,16),(48,0,16,16),(64,0,16,16),(48,0,16,16),(96,0,16,16),(112,0,16,16),(128,0,16,16),]
        self.loadFrames(frameRects)
        self.image=self.frames[self.frameIndex]
        self.rect=self.image.get_rect()
        self.rect.x=260#460
        self.rect.y=51#300
        self.timer=0

    def loadFrames(self,frameRects):
        sheet=setup.GRAPHICS['coins_animation']
        for frameRect in frameRects:
            self.frames.append(tools.get_image(sheet,*frameRect,(0,0,0),C.BG_COIN))#*表解包
    def update(self):
        self.currentTime=pygame.time.get_ticks()
        frameDurations=[125,125,125,125,125,125,125,125]
        if self.timer==0:
            self.timer=self.currentTime
        elif self.currentTime-self.timer>frameDurations[self.frameIndex]:
            self.frameIndex+=1
            self.frameIndex=self.frameIndex%len(frameDurations)
            self.timer=self.currentTime
        self.image=self.frames[self.frameIndex]